<?php
/**
 * Description of BookmarkableToolkit
 *
 * @author andy
 */
class BookmarkableToolkit
{
  protected static $_options = array();

  public static function init(array $options = array())
  {
    if (!empty(self::$_options))
    {
      self::$_options = Doctrine_Lib::arrayDeepMerge(self::$_options, $options);
    }
  }

  public static function addBookmarkCountColumn(Doctrine_Query $q, $column_name = 'bookmark_count', $bookmark_alias_name = 'bk')
  {
    $count_query = Doctrine_Query::create()->select('COUNT(' . $bookmark_alias_name . '.user_id)')
      ->from('Bookmark ' . $bookmark_alias_name)
      ->where($bookmark_alias_name . '.model_component_name = ? AND ' . $bookmark_alias_name . '.model_id = ' . $q->getRootAlias() . '.' . self::$_options['model_pk_name'],
        array($q->getRoot()->getComponentName()));
    $q->addSelect($count_query->getDql() . ' ' . $column_name);
  }
}
